package com.zzptc.easyshop.db;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Myutil {
    /**
     * 读取配置文件xml
     * @return
     * @throws Exception
     */
    public static Map<String,String> readXML()throws Exception{
        Map<String,String> map=new HashMap<String, String>();
        SAXBuilder saxBuilder=new SAXBuilder();
        String path=Myutil.class.getResource("/").getPath()+"db-config.xml";
        Document doc=saxBuilder.build(path);
        //根结点
        Element rootelement=doc.detachRootElement();
        System.out.println("根结点："+rootelement.getName());
        //子节点
        List<Element> listall=rootelement.getChildren();
     //循环
   //     for(int i=0;i<listall.size();i++){
    //        System.out.println("子结点"+i+":"+listall.get(i).getName());
     //   }
        //迭代
        Iterator iter=listall.iterator();//拿到迭代器
        while(iter.hasNext()){//迭代器有几个方法 判断有没有元素hasNext() 有元素就next()
                Element e=(Element) iter.next();
         //   System.out.println(e.getName()+"***"+e.getText());
            //放入map
            map.put(e.getName(),e.getText());
        }

        return  map;
    }

    public static void main(String[] args) {
        try {
            System.out.println(Myutil.readXML().size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
